package cn.bdqn.brand.controller;

import cn.bdqn.brand.service.BrandService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "DelBrandServlet",urlPatterns = "/DelBrandServlet")
public class DelBrandServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 编码格式
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 获取请求参数中的id
        String brandId = request.getParameter("brandId");

        // 类型转换，从string转换为int
        int id = Integer.parseInt(brandId);

        BrandService service = new BrandService();

        boolean rs = service.deleteBrand(id);

        // {code: 200,msg:'删除成功'}
        Map<String,Object> data = new HashMap<>();

        if (rs){
            data.put("code","200");
            data.put("msg","删除成功！");
        }else{
            data.put("code","201");
            data.put("msg","删除失败！请练习管理员！");
        }

        // 转换成json
        String jsonData = JSON.toJSONString(data);

        response.setContentType("application/json");
        response.getWriter().write(jsonData);
    }
}
